package wsi.script;

import java.util.List;

import org.rygh.core.util.UTime;
import org.rygh.core.xml.XmlLeaf;
import org.rygh.core.xml.XmlObject;
import org.rygh.core.xml.XmlPath;
import org.rygh.core.xml.XmlTree;

import wsi.function.Function.Phrase;
import wsi.function.Function.PhraseEx;
import wsi.match.MatchProperty;
import wsi.ws.WebServices;

public class AppScriptInitializer extends AppXml {
	private final static String TEMP = "temp";

	public final static String[][] LOCATIONS = {
	/**/{
	/* 0 */AppParameter.LOCAL_PREFIX + "/runs/" + AppParameter.LOCAL_DIR,
	/* 1 */AppParameter.SERVER_PREFIX + "/runs/" + AppParameter.SERVER_DIR },
	/**/{
	/* 0 */AppParameter.LOCAL_PREFIX + "/gold",
	/* 1 */AppParameter.SERVER_PREFIX + "/gold" },
	/**/{
	/* 0 */AppParameter.LOCAL_PREFIX + "/res",
	/* 1 */AppParameter.SERVER_PREFIX + "/res" },
	/**/{
	/* 0 */AppParameter.LOCAL_PREFIX + "/cache",
	/* 1 */AppParameter.SERVER_PREFIX + "/cache" },
	/**/{
	/* 0 */AppParameter.LOCAL_PREFIX + "/db",
	/* 1 */AppParameter.SERVER_PREFIX + "/db" }, };

	private static void appendFunction(XmlLeaf parent, String name,
			String queryId) {
		appendFunction(parent, name, queryId, true);
	}

	private static void appendFunction(XmlLeaf parent, String name,
			String queryId, boolean on) {
		XmlObject object = new XmlObject(AppTag.function);
		object.addAttribute(AppTag.name, name);
		object.addAttribute(AppTag.switch_on, Boolean.toString(on));
		object.addAttribute(AppTag.input_query, queryId);

		new XmlLeaf(parent, object);
	}

	private static XmlLeaf appendService(XmlLeaf parent, WebServices service,
			boolean on) {
		XmlObject object = new XmlObject(AppTag.web_service);
		object.addAttribute(AppTag.name, service.name());
		object.addAttribute(AppTag.switch_on, Boolean.toString(on));
		return new XmlLeaf(parent, object);
	}

	private static void dependFunction(XmlLeaf parent, String name,
			String dependFunction, String idpred, boolean on,
			MatchProperty... matchProperties) {
		XmlObject object = new XmlObject(AppTag.function);
		object.addAttribute(AppTag.name, name);
		object.addAttribute(AppTag.switch_on, Boolean.toString(on));
		XmlLeaf func = new XmlLeaf(parent, object);

		//

		object = new XmlObject(AppTag.depends_on);
		object.addAttribute(AppTag.id_predicate, idpred);
		XmlLeaf depend = new XmlLeaf(func, object);

		object = new XmlObject(AppTag.function);
		object.addAttribute(AppTag.name, dependFunction);
		XmlLeaf dependfunc = new XmlLeaf(depend, object);

		for (MatchProperty i : matchProperties)
			new XmlLeaf(dependfunc, i.getXmlObject());
	}

	private static void dependFunction(XmlLeaf parent, String name,
			String dependFunction, String idpred,
			MatchProperty... matchProperties) {
		dependFunction(parent, name, dependFunction, idpred, true,
				matchProperties);
	}

	private static void getDeanclatworthy(XmlLeaf parent) {
		XmlLeaf ws = appendService(parent, WebServices.deanclatworthy,
				switchOn("yago", "imdb"));

		appendFunction(ws, "getMovieByName", "movie_1");
		dependFunction(ws, "getMovieByIMDB", "getMovieByName", "t:imdbid",
				MatchProperty.exists());
	}

	private static void getDeezer(XmlLeaf parent) {
		XmlLeaf ws = appendService(parent, WebServices.deezer,
				switchOn("yago", "mbz"));

		appendFunction(ws, "getArtistInfoByName", "singer_1");
		dependFunction(ws, "getArtistInfoById", "getArtistInfoByName", "t:id",
				MatchProperty.exists());
		dependFunction(ws, "getTopTracksByArtistId", "getArtistInfoByName",
				"t:id", MatchProperty.exists());
		dependFunction(ws, "getAlbumsByArtistId", "getArtistInfoByName",
				"t:id", MatchProperty.exists());

		appendFunction(ws, "getAlbumByName", "album_1");
		dependFunction(ws, "getAlbumById", "getAlbumByName", "t:id",
				MatchProperty.exists());
		dependFunction(ws, "getTracksByAlbumId", "getAlbumByName", "t:id",
				MatchProperty.exists());

		dependFunction(ws, "getTrackById", "getTopTracksByArtistId", "t:id",
				MatchProperty.forall("y:created::", YAGO),
				MatchProperty.forall("f:made::", MBZ));

	}

	private static void getDiscogs(XmlLeaf parent) {
		XmlLeaf ws = appendService(parent, WebServices.discogs,
				switchOn("yago", "mbz"));

		appendFunction(ws, "getArtistInfoByName", "singer_1");
		dependFunction(ws, "getArtistInfoById", "getArtistInfoByName", "t:id",
				MatchProperty.exists());
		dependFunction(ws, "getReleasesByArtistId", "getArtistInfoByName",
				"t:id", MatchProperty.exists());

		appendFunction(ws, "getReleaseByName", "album_1");
		appendFunction(ws, "getMasterReleaseByName", "album_1");
		dependFunction(ws, "getReleaseById", "getMasterReleaseByName", "t:id",
				MatchProperty.exists());

	}

	private static void getEchonest(XmlLeaf parent) {
		XmlLeaf ws = appendService(parent, WebServices.echonest,
				switchOn("yago", "mbz"));

		appendFunction(ws, "getArtistInfoByName", "singer_1");
		dependFunction(ws, "getArtistInfoById", "getArtistInfoByName", "t:id",
				MatchProperty.exists());
		// dependFunction(ws, "getSimilarArtistsById", "getArtistInfoByName",
		// "t:id", false, MatchProperty.exists());
		dependFunction(ws, "getTracksByArtistId", "getArtistInfoByName",
				"t:id", MatchProperty.exists());

		appendFunction(ws, "getTracksByArtistName", "singer_1");

		appendFunction(ws, "getTrackByName", "song_1");
		dependFunction(ws, "getTrackById", "getTrackByName", "t:id",
				MatchProperty.exists());
	}

	private static void getGeonames(XmlLeaf parent) {
		XmlLeaf ws = appendService(parent, WebServices.geonames,
				switchOn("yago"));

		appendFunction(ws, "getCountryInfoByTLD", "country_1");
		appendFunction(ws, "getCityByName", "city_1");

		appendFunction(ws, "getCountryByName", "country_2");
		dependFunction(ws, "getNeighboursById", "getCountryByName",
				"t:geonameId", MatchProperty.exists());

	}

	private static void getIsbnDB(XmlLeaf parent) {
		XmlLeaf ws = appendService(parent, WebServices.isbndb, switchOn("yago"));

		appendFunction(ws, "getAuthorInfoByName", "author_1");
		dependFunction(ws, "getAuthorInfoById", "getAuthorInfoByName",
				"t:author_id", MatchProperty.exists());

		appendFunction(ws, "getBookInfoByName", "book_1");
		dependFunction(ws, "getBookInfoByISBN", "getBookInfoByName",
				"y:hasISBN", MatchProperty.exists());

		appendFunction(ws, "getPublisherByName", "publisher_1");
		dependFunction(ws, "getPublisherById", "getPublisherByName",
				"t:publisher_id", MatchProperty.exists());
	}

	private static void getLastfm(XmlLeaf parent) {
		XmlLeaf ws = appendService(parent, WebServices.last_fm,
				switchOn("yago", "mbz"));

		appendFunction(ws, "getArtistInfoByName", "singer_1");
		dependFunction(ws, "getArtistInfoById", "getArtistInfoByName",
				"t:mbid", MatchProperty.exists());
		dependFunction(ws, "getTracksByArtistId", "getArtistInfoByName",
				"t:mbid", MatchProperty.exists());

		appendFunction(ws, "getReleasesByArtistName", "singer_1");
		appendFunction(ws, "getTracksByArtistName", "singer_1");

		appendFunction(ws, "getReleasesByName", "album_1");
		dependFunction(ws, "getTracksByReleaseId", "getReleasesByName",
				"t:mbid", MatchProperty.exists());

		appendFunction(ws, "getTrackByName", "song_1");
		dependFunction(ws, "getTrackById", "getTrackByName", "t:mbid",
				MatchProperty.exists());

	}

	private static void getLibraryThing(XmlLeaf parent) {
		XmlLeaf ws = appendService(parent, WebServices.library_thing,
				switchOn("yago"));

		appendFunction(ws, "getAuthorInfoByName", "author_1");
		dependFunction(ws, "getAuthorInfoById", "getAuthorInfoByName",
				"t:ltml_item_author_-Xat-id", MatchProperty.exists());

		appendFunction(ws, "getBookInfoByName", "book_1");
		dependFunction(ws, "getBookInfoByISBN", "getBookInfoByName",
				"y:hasISBN", MatchProperty.exists());
		dependFunction(ws, "getBookInfoById", "getBookInfoByName",
				"t:ltml_item_-Xat-id", MatchProperty.exists());

	}

	private static void getMusicBrainz(XmlLeaf parent) {
		XmlLeaf ws = appendService(parent, WebServices.music_brainz,
				switchOn("yago", "mbz"));

		appendFunction(ws, "getArtistInfoByName", "singer_1");
		dependFunction(ws, "getArtistInfoById", "getArtistInfoByName",
				"t:-Xat-id", MatchProperty.exists());
		dependFunction(ws, "getReleasesByArtistId", "getArtistInfoByName",
				"t:-Xat-id", MatchProperty.exists());

		appendFunction(ws, "getReleasesByName", "album_1");
		dependFunction(ws, "getReleaseById", "getReleasesByName", "t:-Xat-id",
				MatchProperty.exists());
		dependFunction(ws, "getTracksByReleaseId", "getReleasesByName",
				"t:-Xat-id", MatchProperty.exists());

		appendFunction(ws, "getReleasesByArtistName", "singer_1");
		appendFunction(ws, "getTracksByArtistName", "singer_1");

		appendFunction(ws, "getTracksByName", "song_1");
		dependFunction(ws, "getTrackInfoById", "getTracksByName", "t:-Xat-id",
				MatchProperty.exists());

	}

	private static void getMusixMatch(XmlLeaf parent) {
		XmlLeaf ws = appendService(parent, WebServices.musixmatch,
				switchOn("yago", "mbz"));

		appendFunction(ws, "getArtistInfoByName", "singer_1");
		dependFunction(ws, "getArtistInfoById", "getArtistInfoByName",
				"t:artist_id", MatchProperty.exists());
		dependFunction(ws, "getArtistInfoByMBID", "getArtistInfoByName",
				"t:artist_mbid", MatchProperty.exists());
		// dependFunction(ws, "getSimilarArtistsById", "getArtistInfoByName",
		// "t:artist_id", false, MatchProperty.exists());

		dependFunction(ws, "getAlbumsByArtistId", "getArtistInfoByName",
				"t:artist_id", MatchProperty.exists());
		dependFunction(ws, "getAlbumsByArtistMBID", "getArtistInfoByName",
				"t:artist_mbid", MatchProperty.exists());

		dependFunction(ws, "getAlbumById", "getAlbumsByArtistId", "t:album_id",
				MatchProperty.forall("y:created::", YAGO),
				MatchProperty.forall("f:made::", MBZ));
		dependFunction(ws, "getAlbumByMBID", "getAlbumsByArtistId",
				"t:album_mbid", MatchProperty.forall("y:created::", YAGO),
				MatchProperty.forall("f:made::", MBZ));

		appendFunction(ws, "getTracksByArtistName", "singer_1");
		appendFunction(ws, "getTrackByName", "song_1");
		dependFunction(ws, "getTrackById", "getTracksByArtistName",
				"t:track_id", MatchProperty.forall("y:created::", YAGO),
				MatchProperty.forall("f:made::", MBZ));
		dependFunction(ws, "getTrackByMBID", "getTracksByArtistName",
				"t:track_mbid", MatchProperty.forall("y:created::", YAGO),
				MatchProperty.forall("f:made::", MBZ));

		dependFunction(ws, "getTracksByAlbumId", "getAlbumsByArtistId",
				"t:album_id", MatchProperty.forall("y:created::", YAGO),
				MatchProperty.forall("f:made::", MBZ));
		dependFunction(ws, "getTracksByAlbumMBID", "getAlbumsByArtistId",
				"t:album_mbid", MatchProperty.forall("y:created::", YAGO),
				MatchProperty.forall("f:made::", MBZ));

	}

	private static void getMyMovieAPI(XmlLeaf parent) {
		XmlLeaf ws = appendService(parent, WebServices.mymovieapi,
				switchOn("yago", "imdb"));

		appendFunction(ws, "getMovieByName", "movie_1");
		dependFunction(ws, "getMovieByIMDB", "getMovieByName",
				"t:item_imdb_id", MatchProperty.exists());
	}

	private static void getTrilerAddict(XmlLeaf parent) {
		XmlLeaf ws = appendService(parent, WebServices.triler_addict, false);

		appendFunction(ws, "getActorInfoByName", "actor_1");
		appendFunction(ws, "getMovieInfoByName", "movie_1");
	}

	public static XmlTree initTree(boolean local) {
		XmlTree tree = new XmlTree();

		XmlLeaf app, parent;
		XmlObject object;

		object = new XmlObject(AppTag.application);
		object.addAttribute(AppTag.date, UTime.toTodayString());
		app = new XmlLeaf(tree.getRoot(), object);

		parent = new XmlLeaf(app, new XmlObject(AppTag.configuration));

		putLocations(parent, local);
		putParameters(parent, local);
		putQueriesYago(parent);
		putQueriesMusicBrainz(parent);
		putQueriesIMDB(parent);

		parent = new XmlLeaf(app, new XmlObject(AppTag.workflow));
		getMusicBrainz(parent);
		getDeezer(parent);
		getDiscogs(parent);
		getEchonest(parent);
		getGeonames(parent);
		getIsbnDB(parent);
		getLastfm(parent);
		getLibraryThing(parent);
		getMusixMatch(parent);
		getMyMovieAPI(parent);
		getDeanclatworthy(parent);
		getTrilerAddict(parent);

		return tree;
	}

	private static void putDatabases(XmlLeaf node) {
		XmlLeaf parent = new XmlLeaf(node, new XmlObject(AppTag.databases));

		XmlObject object = new XmlObject(AppTag.kdb);
		object.addAttribute(AppTag.name, YAGO);
		object.addAttribute(AppTag.domain,
				"http://yago-knowledge.org/resource/");
		object.addAttribute(AppTag.functionality_threshold, "0.1");
		new XmlLeaf(parent, object);

		object = new XmlObject(AppTag.kdb);
		object.addAttribute(AppTag.name, MBZ);
		object.addAttribute(AppTag.domain, "http://purl.org/ontology/mo/");
		object.addAttribute(AppTag.functionality_threshold, "0.019");
		new XmlLeaf(parent, object);

		object = new XmlObject(AppTag.kdb);
		object.addAttribute(AppTag.name, IMDB);
		object.addAttribute(AppTag.domain, "http://clem.org/imdb/");
		object.addAttribute(AppTag.functionality_threshold, "0.09");
		new XmlLeaf(parent, object);

		object = new XmlObject(AppTag.kdb);
		object.addAttribute(AppTag.name, TEMP);
		object.addAttribute(AppTag.domain, "http://clem.org/resource/");
		object.addAttribute(AppTag.functionality_threshold, "0.019");
		new XmlLeaf(parent, object);

	}

	private static void putLocations(XmlLeaf parent, boolean local) {
		XmlLeaf child;

		int index = local ? 0 : 1;
		int i = 0;

		child = new XmlLeaf(parent, new XmlObject(AppTag.locations));

		new XmlLeaf(child, new XmlObject(AppTag.runs, LOCATIONS[i++][index]));

		new XmlLeaf(child,
				new XmlObject(AppTag.gold_set, LOCATIONS[i++][index]));

		new XmlLeaf(child, new XmlObject(AppTag.resources,
				LOCATIONS[i++][index]));

		new XmlLeaf(child, new XmlObject(AppTag.cache, LOCATIONS[i++][index]));

		new XmlLeaf(child, new XmlObject(AppTag.kdb, LOCATIONS[i++][index]));
	}

	private static void putParameters(XmlLeaf parent, boolean local) {
		XmlLeaf child;

		child = new XmlLeaf(parent, new XmlObject(AppTag.parameters));

		new XmlLeaf(child, new XmlObject(AppTag.rdb,
				AppParameter.RELATION_DATABASE));
		new XmlLeaf(child, new XmlObject(AppTag.kdb,
				AppParameter.KNOWLEDGE_DATABASE));
		new XmlLeaf(child, new XmlObject(AppTag.jena, AppParameter.JENA));
		new XmlLeaf(child, new XmlObject(AppTag.web_cache_on,
				Boolean.toString(AppParameter.WEB_CACHE_ON)));
		new XmlLeaf(child, new XmlObject(AppTag.input_size,
				Integer.toString(local ? AppParameter.LOCAL_INPUT_SIZE
						: AppParameter.SERVER_INPUT_SIZE)));
		new XmlLeaf(child, new XmlObject(AppTag.degree_limit,
				Integer.toString(AppParameter.DEGREE_LIMIT)));
		new XmlLeaf(child, new XmlObject(AppTag.hop,
				Integer.toString(local ? AppParameter.LOCAL_HOP
						: AppParameter.SERVER_HOP)));
		new XmlLeaf(child, new XmlObject(AppTag.resolution_threshold, "0.9"));
		new XmlLeaf(child, new XmlObject(AppTag.input_selection,
				AppParameter.INPUT_SELECT_TYPE.name()));

		putDatabases(child);
		putPhrases(child);
	}

	private static void putPhrases(XmlLeaf parent) {
		XmlLeaf phrases = new XmlLeaf(parent, new XmlObject(AppTag.phrases));

		XmlObject object = new XmlObject(AppTag.phrase,
				PhraseEx.SAMPLING.name());
		object.addAttribute(AppTag.switch_on, "true");
		new XmlLeaf(phrases, object);

		// object = new XmlObject(Tag.phrase, Phrase.YAGO_HOP);
		// object.addAttribute(Tag.switch_on, "false");
		// new XmlLeaf(phrases, object);

		object = new XmlObject(AppTag.phrase, PhraseEx.SELECTED_TREES.name());
		object.addAttribute(AppTag.switch_on,
				Boolean.toString(AppParameter.SELECTED_TREES_ON));
		new XmlLeaf(phrases, object);

		object = new XmlObject(AppTag.phrase, Phrase.RESOLVE.name());
		object.addAttribute(AppTag.switch_on,
				Boolean.toString(AppParameter.RESOLVE_ON));
		new XmlLeaf(phrases, object);

		object = new XmlObject(AppTag.phrase, Phrase.REST.name());
		object.addAttribute(AppTag.switch_on, "true");
		new XmlLeaf(phrases, object);
	}

	private static void putQueriesIMDB(XmlLeaf parent) {
		XmlLeaf child;
		XmlObject object;

		object = new XmlObject(AppTag.queries);
		object.addAttribute(AppTag.kdb, IMDB);
		child = new XmlLeaf(parent, object);

		object = new XmlObject(AppTag.input_query, "?y y:actedIn ?x. ");
		object.addAttribute(AppTag.id, "movie_1");
		object.addAttribute(AppTag.max, "130");
		object.addAttribute(AppTag.type, "y:wordnet_movie_106613686");
		object.addAttribute(AppTag.name_predicate, "y:hasLabel");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query, "?x y:actedIn ?y. ");
		object.addAttribute(AppTag.id, "actor_1");
		object.addAttribute(AppTag.max, "240");
		object.addAttribute(AppTag.type, "y:wordnet_person_100007846");
		object.addAttribute(AppTag.name_predicate, "y:hasLabel");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query, "?x y:directed ?y. ");
		object.addAttribute(AppTag.id, "director_1");
		object.addAttribute(AppTag.max, "240");
		object.addAttribute(AppTag.type, "y:wordnet_person_100007846");
		object.addAttribute(AppTag.name_predicate, "y:hasLabel");
		new XmlLeaf(child, object);

	}

	private static void putQueriesMusicBrainz(XmlLeaf parent) {
		XmlLeaf child;
		XmlObject object;

		object = new XmlObject(AppTag.queries);
		object.addAttribute(AppTag.kdb, MBZ);
		child = new XmlLeaf(parent, object);

		object = new XmlObject(AppTag.input_query, "?x w:type y:MusicArtist. "
				+ "?x f:made ?z. ");
		object.addAttribute(AppTag.id, "singer_1");
		object.addAttribute(AppTag.max, "4500");
		object.addAttribute(AppTag.type, "y:MusicArtist");
		object.addAttribute(AppTag.name_predicate, "f:name");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query, "?x w:type y:Release. "
				+ "?y f:made ?x.");
		object.addAttribute(AppTag.id, "album_1");
		object.addAttribute(AppTag.max, "13");
		object.addAttribute(AppTag.type, "y:Release");
		object.addAttribute(AppTag.name_predicate, "u:label");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query, "?t y:published_as ?x. "
				+ "?t w:type y:Track. " + "?y f:made ?t.");
		object.addAttribute(AppTag.id, "song_1");
		object.addAttribute(AppTag.max, "22");
		object.addAttribute(AppTag.type, "y:Track");
		object.addAttribute(AppTag.name_predicate, "u:label");
		object.addAttribute(AppTag.link_predicate, "y:published_as");
		new XmlLeaf(child, object);

	}

	private static void putQueriesYago(XmlLeaf parent) {
		XmlLeaf child;
		XmlObject object;

		object = new XmlObject(AppTag.queries);
		object.addAttribute(AppTag.kdb, YAGO);
		child = new XmlLeaf(parent, object);

		object = new XmlObject(AppTag.input_query,
				"?x w:type y:wordnet_singer_110599806. " + "?x y:created ?z. ");
		object.addAttribute(AppTag.id, "singer_1");
		object.addAttribute(AppTag.type, "y:wordnet_person_100007846");
		object.addAttribute(AppTag.name_predicate, "y:hasPreferredName");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query,
				"?x w:type y:wordnet_album_106591815. "
						+ "?y y:created ?x. ?y y:hasWonPrize ?z.");
		object.addAttribute(AppTag.id, "album_1");
		object.addAttribute(AppTag.type, "y:wordnet_album_106591815");
		object.addAttribute(AppTag.name_predicate, "y:hasPreferredName");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query,
				"?x w:type y:wordnet_song_107048000. "
						+ "?y y:created ?x. ?y y:hasWonPrize ?z.");
		object.addAttribute(AppTag.id, "song_1");
		object.addAttribute(AppTag.type, "y:wordnet_song_107048000");
		object.addAttribute(AppTag.name_predicate, "y:hasPreferredName");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query,
				"?x y:hasWonPrize y:Nobel_Prize_in_Literature.");
		object.addAttribute(AppTag.id, "author_1");
		object.addAttribute(AppTag.type, "y:wordnet_person_100007846");
		object.addAttribute(AppTag.name_predicate, "y:hasPreferredName");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query,
				"?x w:type y:wordnet_book_106410904. " + "?y y:created ?x");
		object.addAttribute(AppTag.id, "book_1");
		object.addAttribute(AppTag.type, "y:wordnet_book_106410904");
		object.addAttribute(AppTag.name_predicate, "y:hasPreferredName");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query,
				"?x w:type y:wordnet_actor_109765278. "
						+ "?x y:actedIn ?y. ?x y:hasWonPrize ?z.");
		object.addAttribute(AppTag.id, "actor_1");
		object.addAttribute(AppTag.type, "y:wordnet_person_100007846");
		object.addAttribute(AppTag.name_predicate, "y:hasPreferredName");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query,
				"?x w:type y:wordnet_movie_106613686. "
						+ "?y y:actedIn ?x. ?y y:hasWonPrize ?z. ");
		object.addAttribute(AppTag.id, "movie_1");
		object.addAttribute(AppTag.type, "y:wordnet_movie_106613686");
		object.addAttribute(AppTag.name_predicate, "y:hasPreferredName");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query,
				"?x w:type y:wordnet_publisher_108062623. ");
		object.addAttribute(AppTag.id, "publisher_1");
		object.addAttribute(AppTag.type, "y:wordnet_publisher_108062623");
		object.addAttribute(AppTag.name_predicate, "y:hasPreferredName");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query,
				"?x w:type y:yagoGeoEntity. ?x y:hasTLD ?y.");
		object.addAttribute(AppTag.id, "country_1");
		object.addAttribute(AppTag.type, "y:wordnet_country_108544813");
		object.addAttribute(AppTag.name_predicate, "y:hasTLD");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query,
				"?x w:type y:wordnet_country_108544813. ?x u:label ?y.");
		object.addAttribute(AppTag.id, "country_2");
		object.addAttribute(AppTag.type, "y:wordnet_country_108544813");
		object.addAttribute(AppTag.name_predicate, "u:label");
		new XmlLeaf(child, object);

		object = new XmlObject(AppTag.input_query,
				"?x w:type y:wordnet_administrative_district_108491826. ?x u:label ?y.");
		object.addAttribute(AppTag.id, "city_1");
		object.addAttribute(AppTag.type,
				"y:wordnet_administrative_district_108491826");
		object.addAttribute(AppTag.name_predicate, "u:label");
		new XmlLeaf(child, object);
	}

	private static boolean switchOn(String... dbs) {
		for (String db : dbs)
			if (db.equals(AppParameter.KNOWLEDGE_DATABASE))
				return true;

		return false;
	}

	public void turn(WebServices service, boolean on) {
		List<XmlLeaf> nodes = tree.getNodesByXPath(XmlPath.compose(
				AppTag.application, AppTag.workflow, AppTag.web_service));

		for (XmlLeaf node : nodes) {
			XmlObject obj = node.getValue();

			if (obj.getAttribute(AppTag.name).equals(service.name())) {
				obj.addAttribute(AppTag.switch_on, Boolean.toString(on));
			}
		}
	}

	public void turnAll(boolean on) {
		for (WebServices ws : WebServices.values()) {
			turn(ws, on);
			turnFunctions(ws, on);
		}
	}

	public void turnFunctions(WebServices service, boolean on) {
		List<XmlLeaf> nodes = tree.getNodesByXPath(XmlPath.compose(
				AppTag.application, AppTag.workflow, AppTag.web_service));

		for (XmlLeaf node : nodes) {
			XmlObject obj = node.getValue();
			obj.addAttribute(AppTag.switch_on, Boolean.toString(on));

			if (obj.getAttribute(AppTag.name).equals(service.name())) {
				List<XmlLeaf> functions = node.getChildren();

				for (XmlLeaf f : functions)
					f.getValue().addAttribute(AppTag.switch_on,
							Boolean.toString(on));
			}
		}
	}

	public void turnServices(boolean on) {
		List<XmlLeaf> nodes = tree.getNodesByXPath(XmlPath.compose(
				AppTag.application, AppTag.workflow, AppTag.web_service));

		for (XmlLeaf node : nodes) {
			XmlObject obj = node.getValue();

			obj.addAttribute(AppTag.switch_on, Boolean.toString(on));
		}
	}
}
